package cn.tedu.controller;

import cn.tedu.controller.BaseAndEx.BaseController;
import cn.tedu.dao.GoodsCategoryMapper;
import cn.tedu.entity.Goods;
import cn.tedu.entity.GoodsCategory;
import cn.tedu.service.port.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/main")
public class MainController extends BaseController {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    @Resource
    private GoodsService goodsService;

    /**
     * 主页跳转
     * @return
     */
    @RequestMapping("/index.do")
    public String showIndex(ModelMap modelMap){
        List<List<GoodsCategory>> computerCategories = new ArrayList<>();
        //此集合装了id为161的3个2级分类,然后添加到大集合里面去
        List<GoodsCategory> categories161 = goodsCategoryMapper.getGoodsCatgoryListByParentId(161,0,3);

        //遍历3个2级分类，或得2级分类里面的所有3级分类,然后添加到大集合里面去
        List<GoodsCategory> subCategories;
        for (GoodsCategory goodsCategory: categories161){
            subCategories = goodsCategoryMapper.getGoodsCatgoryListByParentId(goodsCategory.getId(),null,null);
            computerCategories.add(subCategories);
        }

        List<Goods> computers = goodsService.getGoodsListByCategoryId(163,0,3);

//        //遍历 排名电脑排名前3
//        for (Goods computerss:computers) {
//            System.out.println("排名前3:"+computerss);
//        }

//        //遍历2级分类
//        for (GoodsCategory category:categories161) {
//            System.out.println("\t" + category.getName());
//        }
//        System.out.println();
//
//        //遍历3级分类
//        for (List<GoodsCategory> testCategories :computerCategories) {
//            for (GoodsCategory goodsCategory:testCategories) {
//                System.out.println("\t" + goodsCategory.getName());
//            }
//            System.out.println();
//        }
        modelMap.addAttribute("computerCategories", computerCategories);
        modelMap.addAttribute("categories161", categories161);
        modelMap.addAttribute("computers", computers);

        return "index";
    }
}
